package com.cos.lession73.slice;

import com.cos.lession73.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.Text;
import ohos.multimodalinput.event.MmiPoint;
import ohos.multimodalinput.event.TouchEvent;

public class Touch2AbilitySlice extends AbilitySlice {


    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        setUIContent(ResourceTable.Layout_ability_touch2);

        DirectionalLayout layout = (DirectionalLayout) findComponentById(ResourceTable.Id_fu_dir);

        //绑定触摸事件
        layout.setTouchEventListener(this::onTouchEvent);

    }
    //作为属性
    float startX=0;
    float startY=0;
    public boolean onTouchEvent(Component component, TouchEvent touchEvent) {
        int action = touchEvent.getAction();
        Text text = (Text) findComponentById(ResourceTable.Id_touch_text);
        if (action== TouchEvent.PRIMARY_POINT_DOWN) {
            MmiPoint pp = touchEvent.getPointerPosition(0);
            //局部变量会被回收
            startX = pp.getX();
            startY = pp.getY();
            text.setText("x:"+startX+",y:"+startY);
        }else if(action==TouchEvent.POINT_MOVE ){//只要按着移动就会一直触发
//            MmiPoint pp = touchEvent.getPointerPosition(0);
//           float x = pp.getX();
//            float y = pp.getY();
                   // text.setText("手指在移动:x:"+x+",y:"+y);
        }else if(action==TouchEvent.PRIMARY_POINT_UP){
            MmiPoint pp2 = touchEvent.getPointerPosition(0);
            float endX = pp2.getX();
            float endY = pp2.getY();
//                    text.setText("手指离开了:x:"+x+",y:"+y);
//           _ startY endY  +
//            - endX startX +   100 范围
            if(endX>startX&&Math.abs(endY-startY)<100){
                text.setText("向右sx:"+startX+",sy:"+startY+"---ex:"+endX+",ey:"+endY);
            }else if(endX<startX&&Math.abs(endY-startY)<100){
                text.setText("向左sx:"+startX+",sy:"+startY+"---ex:"+endX+",ey:"+endY);
            }else if (endY>startY&&Math.abs(endX-startX)<100){
                text.setText("向下sx:"+startX+",sy:"+startY+"---ex:"+endX+",ey:"+endY);
            }else if(endY<startY&&Math.abs(endX-startX)<100){
                text.setText("向上sx:"+startX+",sy:"+startY+"---ex:"+endX+",ey:"+endY);
            }
        }
        return true;
    }
}
